#ifndef HEAD_H
#define HEAD_H
#include<iostream>
using namespace std;

struct node{
    node* nx;
    node*pre;
    int data;
};

class arraylist{
private:
    node*head;
    node*tail;
    int len;
public:
    arraylist();
    ~arraylist();
    void creatlist();
    void showlist();
    void deletenode(int);
    node*findnode(int);
};

node* arraylist::findnode(int nodeToDelete){
    node*q=head;
    while(q->nx!=head){
        q=q->nx;
        if(q->data==nodeToDelete)
            return q;
    }
    cout<<endl<<"no such node!";
    return NULL;
}

void arraylist::deletenode(int nodeToDelete){
    node*target=findnode(nodeToDelete);
//    if(target!=NULL){
//        node*pret=target->pre;
//        node*next=target->nx;
//        pret->nx=next;
//        next->pre=pret;
//        delete target;
//        len--;
//    }
    node*pret=head;
    while(pret->nx!=target){
        pret=pret->nx;
    }
    node*nextt=target->nx;
    pret->nx=nextt;
    nextt->pre=pret;
    delete target;
    len--;
    if(!len){
        tail=head->nx=head->pre=NULL;
    }
}

arraylist::arraylist(){
    cout<<"Input len:";
    cin>>len;
    creatlist();
}

void arraylist::creatlist(){
    node*q=head=tail=new node;
    node*preq;
    cout<<endl<<"input elements:";
    int i=0;
    while(i<len){
        preq=q;
        tail=q=q->nx=new node;
        cin>>q->data;
        q->pre=q;
        i++;
    }
    head->pre=tail;
    tail->nx=head;
}

void arraylist::showlist(){
    node*q=head;
    int i=0;
    while(i<len){
        q=q->nx;
        cout<<q->data<<"\t";
        i++;
    }
}

arraylist::~arraylist(){
    node*q=head;
    while(head){
        q=head;
        head=head->nx;
        delete q;
    }
    head=tail=NULL;
}

#endif // HEAD_H
